unit mapagoogle_unt;

interface

uses
  Funcoes,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, OleCtrls, SHDocVw,
  urlmon, DB, IBCustomDataSet, IBQuery, GMLinkedComponents, GMMarker,
  GMMarkerVCL, GMMap, GMGeoCode, GMClasses, GMMapVCL;

type
  Tmapagoogle_frm = class(TForm)
    WebBrowser1: TWebBrowser;
    GMMap1: TGMMap;
    GMGeoCode1: TGMGeoCode;
    GMMarker1: TGMMarker;
    IBQGoogleMapas: TIBQuery;
    IBQGoogleMapasENDERECO: TIBStringField;
    dsGoogleMapas: TDataSource;
    procedure IBQGoogleMapasAfterOpen(DataSet: TDataSet);
    procedure GMMap1AfterPageLoaded(Sender: TObject; First: Boolean);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
  public
  end;

var
  mapagoogle_frm: Tmapagoogle_frm;

implementation

{$R *.dfm}

Uses DMSistema_unt, DMDados_unt, principal_unt, cadastrodomicilios_unt;

procedure Tmapagoogle_frm.IBQGoogleMapasAfterOpen(DataSet: TDataSet);
begin
   with IBQGoogleMapas do
   begin
      try
         Prepare;
         FetchAll;
         First;
      except
      end;
   end;
   if not IBQGoogleMapas.IsEmpty then
   begin
      GMMap1.Active := True;
      GMGeoCode1.Geocode(AllTrim(MudaNome(retirasinais(IBQGoogleMapasENDERECO.AsString))));
      GMMarker1.Items[dsGoogleMapas.DataSet.RecNo].CenterMapToMarker;
   end;
end;

procedure Tmapagoogle_frm.GMMap1AfterPageLoaded(Sender: TObject;
  First: Boolean);
begin
   if not (DMDados_frm.IBDSDomicilio.State in ([dsInsert, dsEdit])) then
   begin
      GMMap1.DoMap;
   end;
end;

procedure Tmapagoogle_frm.FormCreate(Sender: TObject);
begin
   if not (DMDados_frm.IBDSDomicilio.State in ([dsInsert, dsEdit])) then
   begin
      if (not DMDados_frm.IBDSDomicilio.IsEmpty) then
      begin
         if Length(AllTrim(DMDados_frm.IBDSDomicilioENDERECO.Value)) > 0 then
         begin
            with IBQGoogleMapas do
            begin
               try
                  Close;
                  SQL.Clear;
                  if DMDados_frm.IBDSDomicilioIDZONA.Value = 1 then
                  begin
                     if DMDados_frm.IBDSDomicilioNUMERO.Value > 0 then
                     begin
                        SQL.Add('SELECT A.ENDERECO||'+', '+'||CAST(A.NUMERO AS VARCHAR(5))||'+', '+'||A.BAIRRO||'+', '+'||B.CIDADE||'+', '+'||B.ESTADO AS ENDERECO FROM DOMICILIO A, PARAMETROS B WHERE A.IDDOMICILIO = :IDOMICILIO');
                     end
                     else
                     begin
                        SQL.Add('SELECT A.ENDERECO||'+', '+'||A.BAIRRO||'+', '+'||B.CIDADE||'+', '+'||B.ESTADO AS ENDERECO FROM DOMICILIO A, PARAMETROS B WHERE A.IDDOMICILIO = :IDOMICILIO');
                     end;
                  end
                  else
                  begin
                     SQL.Add('SELECT A.ENDERECO||'+', '+'||B.CIDADE||'+', '+'||B.ESTADO AS ENDERECO FROM DOMICILIO A, PARAMETROS B WHERE A.IDDOMICILIO = :IDOMICILIO');
                  end;
                  parambyname('iddomicilio').asinteger := DMDados_frm.IBDSDomicilioIDDOMICILIO.Value;
                  Prepare;
                  Open;
               except
               end;
            end;
         end;
      end;
   end;
end;

procedure Tmapagoogle_frm.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
   mapagoogle_frm := nil;
   Action := caFree;
end;

end.

